//
//  ZLUIKit.swift
//  jingxin
//
//  Created by Yuki on 2020/8/5.
//  Copyright © 2020 Zl. All rights reserved.
//

import UIKit

/// 创建一个按钮
/// - Parameters:
///   - title: 按钮标题
///   - fontSize: 字体大小
///   - titleColor: 标题颜色
/// - Returns: 按钮
func createBtn(withTitle title:String,withFontSize fontSize:CGFloat,withTitleColor titleColor:UIColor) ->UIButton{
    let btn = UIButton.init(type:.custom);
    btn.setTitle(title, for: UIControl.State.normal);
    btn.titleLabel?.font = UIFont.systemFont(ofSize: fontSize);
    btn.setTitleColor(titleColor, for: UIControl.State.normal);
    return btn;
}

/// 创建图片
/// - Parameter imgName: 图片名称
/// - Returns: UIImageView
func createImgView(_ imgName:String)->UIImageView{
    let imgView:UIImageView = UIImageView.init();
    imgView.contentMode = .scaleAspectFill;//拉伸填充，会裁剪
    imgView.image = UIImage.init(named: imgName);
    return imgView;
}


/// 创建导航栏的左右按钮
/// - Parameters:
///   - title: 标题 可以为空
///   - imageName: 图片名称 可以为空
///   - target: target
///   - selector: 点击方法
/// - Returns: UIBarButtonItem
func createBarItem(withTitle title:String,withImageName imageName:String ,withTarget target:Any ,withSelector selector:Selector) ->UIBarButtonItem{
    
    /**  判断图片是否为空  **/
    if IsEmptyStr(checkStr: imageName as AnyObject){
        /**  图片为空  **/
        let barItem = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.done, target: target, action: selector);
        barItem.tintColor = UIColor.black;
        return barItem;
    }else{
        
        if IsEmptyStr(checkStr: title as AnyObject){
            /**  文字为空  **/
            /**  UIImage.RenderingMode.alwaysOriginal == .alwaysOriginal  **/
            let barItem = UIBarButtonItem(image: UIImage(named: imageName)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), style: .done, target: target, action: selector);
            return barItem;
        }else{
            /**  图片文字都有  **/
            let btn = UIButton.init(type: .custom);
            btn.setTitle(title, for: .normal);
            btn.setImage(UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal), for:.normal);
            btn.setTitleColor(UIColor.black, for: .normal);
            btn.addTarget(target, action: selector, for: .touchUpInside);
            let barItem = UIBarButtonItem(customView: btn);
            return barItem;
        }
    }
}

